package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.os.Build;
import android.text.Html;
import android.text.Layout;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.RMain;

/**
 * Created by xxx on 2018/1/23.
 */

public class RightMenuAdapter extends BaseAdapter{

    private Context context;
    private String[] list;
    private int selectItem = -1;
    private int oldPostion = -1;
    private RMain.QBean qBean;
    public RightMenuAdapter(Context context, String[] list, RMain.QBean qBean){
        this.context = context;
        this.list = list;
        this.qBean = qBean;
    }

    @Override
    public int getCount() {
        return list!=null?list.length:0;
    }

    @Override
    public Object getItem(int position) {
        return list[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setSelectItem(int position){
        selectItem = position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.right_menu_item, parent, false);
            holder = new ViewHolder();
            holder.itemName = convertView.findViewById(R.id.itemName);
            holder.hideArea = convertView.findViewById(R.id.hideArea);
            holder.textHtml = convertView.findViewById(R.id.textHtml);
            holder.rightArrow = convertView.findViewById(R.id.rightArrow);
            holder.leftImg = convertView.findViewById(R.id.leftImg);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.itemName.setText(list[position]);
        holder.hideArea.setVisibility(View.GONE);
        if (selectItem == position && position == 0) { //第一项展开
            if (qBean != null) {
                holder.hideArea.setVisibility(View.VISIBLE);
                holder.rightArrow.animate().setDuration(200).rotation(90).start();
                if (qBean.getDescription() != null && !TextUtils.isEmpty(qBean.getDescription())) {
                    //0 visible ,4 invisible 8 gone
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                        holder.textHtml.setText(Html.fromHtml(qBean.getDescription(), Html.FROM_HTML_MODE_COMPACT));
                    } else {
                        holder.textHtml.setText(Html.fromHtml(qBean.getDescription()));
                    }
                } else {
                    holder.textHtml.setText("暂无评估信息说明");
                }
            }
            notifyDataSetChanged();
        }
        holder.hideArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (position == selectItem){
                    selectItem = -1;
                    holder.rightArrow.animate().setDuration(200).rotation(90).start();
                }
                notifyDataSetChanged();
            }
        });
        if (position==0){
            holder.leftImg.setImageResource(R.mipmap.icon_state);
        }
        else if (position == 1){
            holder.leftImg.setImageResource(R.mipmap.icon_information_hdpi);
        }else if (position==2){
            holder.leftImg.setImageResource(R.mipmap.icon_trace);
        }else{
            holder.leftImg.setImageResource(R.mipmap.icon_sign_menu);
        }
        notifyDataSetChanged();
    return convertView;
}
class ViewHolder{
    TextView itemName;
    TextView textHtml;
    LinearLayout hideArea;
    ImageView leftImg;
    ImageView rightArrow;
}
}
